package ntut.andengine.physics;

import javax.microedition.khronos.opengles.GL10;

import ntut.andengine.game.GameLevel;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class Box extends PhysicalComponent {

	boolean isVanish = false;
	GameLevel mLevel;
	PhysicsConnector boxPhysicsConnector;
	//Box box;
	
	public Box(GameLevel level) {
		super(level);

		mLevel = level;
		this.mBitmapTexture = new BitmapTexture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion boxTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, level, "box.png", 0, 0);
		level.getEngine().getTextureManager().loadTexture(this.mBitmapTexture);	
		
		mSprite = new Sprite(0, 0, boxTextureRegion);
		mSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		
		FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		mBody = PhysicsFactory.createBoxBody(level.getPhysicsWorld(), mSprite, BodyType.StaticBody, fixtureDef);
		mBody.setUserData(new ContactEvent("Box"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				try{
					if(body.getUserData().toString() == "Player"){
						mBody.setType(BodyType.DynamicBody);
					}
					if(body.getUserData().toString() == "DeathLight"){
						
						isVanish = true;
						
						//mSprite.registerEntityModifier(new AlphaModifier(1, 1, 0));
						mSprite.registerUpdateHandler(new IUpdateHandler(){

							@Override
							public void onUpdate(float pSecondsElapsed) {

								//if(mSprite.getAlpha() == 0) {
								
//									mBody.setActive(false);
								mBody.setType(BodyType.StaticBody);
								setPosition(new Vector2(-100, -100));
								
//								mLevel.getPhysicsWorld().unregisterPhysicsConnector(boxPhysicsConnector);
//								mLevel.getPhysicsWorld().destroyBody(mBody);
//								mLevel.getScene().detachChild(mSprite);
								//}
							}

							@Override
							public void reset() {
								// TODO Auto-generated method stub
								
							}
							
						});
					}
					else{
						isVanish = false;
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				try{
					if(body.getUserData().toString() == "Player"){
						mBody.setType(BodyType.StaticBody);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}});
		
		level.getScene().attachChild(mSprite);
		boxPhysicsConnector = new PhysicsConnector(mSprite, mBody, true, true);
		level.getPhysicsWorld().registerPhysicsConnector(boxPhysicsConnector);
		level.addMovingComponent(this);
		level.addDeathBoxList(this);
		//box = this;
	}

	@Override
	public void changeToRed(){
		
		if (mSprite == null) return;

        if (mColor == ComponentColor.Red) mSprite.setColor(1, 0, 0, 1);
        else if (mColor == ComponentColor.Blue) mSprite.setColor(1f, 1f, 1f, 0.5f);
        else if (mColor == ComponentColor.White) mSprite.setColor(1, 1, 1, 1);
	}
	
	@Override
	public void changeToBlue(){
		
		if (mSprite == null) return;

        if (mColor == ComponentColor.Red) mSprite.setColor(1f, 1f, 1f, 0.5f);
        else if (mColor == ComponentColor.Blue) mSprite.setColor(0, 0, 1, 1);
        else if (mColor == ComponentColor.White) mSprite.setColor(1, 1, 1, 1);
	}
	
	public void recovery(PauseablePhysicsWorld physicsWorld){//Vector2 pos
		if(isVanish){
			PhysicalFactory.CreateBox(mLevel, this.getPosition(), this.getColor());
			physicsWorld.unregisterPhysicsConnector(boxPhysicsConnector);
			physicsWorld.destroyBody(mBody);
			mLevel.getScene().detachChild(mSprite);
			
		}
//		isVanish = false;
//		setPosition(pos);
//		Log.d("Box", getPosition().toString());
	}
	
	public boolean getVanish(){
		return isVanish;
	}
	
	public void setVanish(boolean flag){
		isVanish = flag;
	}
}
